<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="部门编码" prop="deptCode">
      <el-input v-model="dataForm.deptCode" placeholder="部门编码"></el-input>
    </el-form-item>
    <el-form-item label="部门名称" prop="deptName">
      <el-input v-model="dataForm.deptName" placeholder="部门名称"></el-input>
    </el-form-item>
    <el-form-item label="父级部门" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父级部门"></el-input>
    </el-form-item>
    <el-form-item label="是否删除，0 否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除，0 否 1是"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deptCode: '',
          deptName: '',
          parentId: '',
          isDelete: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          deptCode: [
            { required: true, message: '部门编码不能为空', trigger: 'blur' }
          ],
          deptName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父级部门不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除，0 否 1是不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/dept/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deptCode = data.dept.deptCode
                this.dataForm.deptName = data.dept.deptName
                this.dataForm.parentId = data.dept.parentId
                this.dataForm.isDelete = data.dept.isDelete
                this.dataForm.createTime = data.dept.createTime
                this.dataForm.updateTime = data.dept.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/dept/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deptCode': this.dataForm.deptCode,
                'deptName': this.dataForm.deptName,
                'parentId': this.dataForm.parentId,
                'isDelete': this.dataForm.isDelete,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
